package com.gdpu.config;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/6/22
 */
@Configuration
@EnableRabbit
public class RabbitMqConfig {

    @Bean
    public CachingConnectionFactory connectionFactory(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost("localhost");
        cachingConnectionFactory.setPort(5672);
        cachingConnectionFactory.setUsername("guest");
        cachingConnectionFactory.setPassword("guest");
        cachingConnectionFactory.setVirtualHost("myMall");
        return  cachingConnectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(cachingConnectionFactory);
        return rabbitTemplate;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(RabbitTemplate rabbitTemplate){
        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
        return rabbitAdmin;
    }


    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(CachingConnectionFactory cachingConnectionFactory){
        //SimpleRabbitListenerContainerFactory发现消息中有content_type有text就会默认将其转换成string类型的
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(cachingConnectionFactory);
        return factory;
    }

    /**
     * 创建普通交换机
     * @return
     */
    @Bean
    public DirectExchange orderExchange(){return new DirectExchange("orderExchange",true,false);};

    /**
     * 创建定时队列
     * @return
     */
    @Bean
    public Queue orderTTLQueue(){
        Map<String, Object> map = new HashMap<>(2);
//        map.put("x-message-ttl",10000);
        // 过期队列和私信队列绑定
        map.put("x-dead-letter-exchange","ttl_dead_order_exchange");
        map.put("x-dead-letter-routing-key","ttlDeadOrderKey");
        return new Queue("ttl.order.queue",true,false,false, map);
    }

    /**
     * 绑定定时队列和交换机
     * @return
     */
    @Bean
    public Binding ttlBinds(){
        return BindingBuilder.bind(orderTTLQueue()).to(orderExchange()).with("ttlMessage");
    }

    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public DirectExchange deadDirectExchange(){return new DirectExchange("ttl_dead_order_exchange",true,false);};

    /**
     * 死信队列,过期消息存放的地方
     * @return
     */
    @Bean
    public Queue deadQueue(){
        return new Queue("ttl.dead.order.queue",true);
    }

    @Bean
    public Binding deadBinds(){
        return BindingBuilder.bind(deadQueue()).to(deadDirectExchange()).with("ttlDeadOrderKey");
    }

}
